package com.myapp.struts.DAO;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Tarde
 */
public class MiSql {

    private static Connection conn = null;
    private static Statement stm = null;
    private static String BbDd = "struts_practica5";
    private static String usuario = "root";
    private static String pass = "forman";

    public MiSql() {
    }

    /**
     * Clase conectar
     */
    public void conectar() {

        try {
            //crear el driver 
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            //obtener el manejador 
            conn = DriverManager.getConnection("jdbc:mysql://localhost/" + BbDd, usuario, pass);

            //obtener el statement 
            stm = conn.createStatement();

        } catch (SQLException err) {
            System.out.println("Error SQLException: " + err.getMessage() + "\n" + err.toString());

        } catch (Exception e) {
            System.out.println(e.getMessage());

        } finally {
            //cerrar la conexion 
        }
    }
    
    public void cerrarStm() {

        if (stm != null) {
            try {
                stm.close();
            } catch (Exception e) {
            }
        }
    }
    
    public void cerrarConexion() {

        if (conn != null) {
            try {
                stm.close();   
                conn.close();
            } catch (Exception e) {
            }
        }
    }

    public int updateMiSQL(String consulta) {
        int rst = 0;
        try {
            rst = stm.executeUpdate(consulta);

        } catch (SQLException err) {
            System.out.println("Error SQLException: " + err.getMessage() + "\n" + err.toString());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            return rst;
        }
    }

    public ResultSet selectMiSQL(String consulta) {
        ResultSet rst = null;
        try {
            rst = stm.executeQuery(consulta);
            
        } catch (SQLException err) {
            System.out.println("Error SQLException: " + err.getMessage() + "\n" + err.toString());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            return rst;
        }
    }

    public int selectCountMiSQL(String consulta) {
        ResultSet rst;
        int count = 0;
        try {
            rst = stm.executeQuery(consulta);
            rst.next();
            count = Integer.parseInt(rst.getString(1));

        } catch (SQLException err) {
            System.out.println("Error SQLException: " + err.getMessage() + "\n" + err.toString());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            return count;
        }
    }
}
